import UIKit
import MBProgressHUD

typealias XBlock = (()->())
class XXHudManager: NSObject {
    static let shared = XXHudManager.init()
    var loadingHUD: MBProgressHUD?
    class func showSuccess(msg:String!) {
        guard let view = self.getFatherView() else {return}
        self.showSuccess(msg: msg, to: view)
    }
    class func showSuccess(msg:String!, to view:UIView) {
        self.showMessage(msg: msg, to: view)
    }
    class func showWarn(msg:String!) {
        guard let view = self.getFatherView() else {return}
        self.showWarn(msg: msg, to: view)
    }
    class func showWarn(msg:String!, to view:UIView) {
        self.showMessage(msg: msg, to: view)
    }
    class func showError(msg:String!) {
        guard let view = self.getFatherView() else {return}
        self.showError(msg: msg, to: view)
    }
    class func showError(msg:String!, to view:UIView) {
        self.showMessage(msg: msg, to: view)
    }
    class func showErrorAlert(errMsg:String!) {
        guard let view = self.getFatherView() else {return}
        self.showErrorAlert(errMsg: errMsg, to: view)
    }
    class func showErrorAlert(errMsg:String!, to view:UIView) {
        DispatchQueue.main.async {
            let alert = UIAlertController.init(title: "温馨提示", message: errMsg, preferredStyle: UIAlertController.Style.alert)
            let action = UIAlertAction.init(title: "我知道了", style: UIAlertAction.Style.cancel, handler: nil)
            alert.addAction(action)
            UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
        }
    }
    class func showMessage(msg:String!) {
        guard let view = self.getFatherView() else {return}
        self.showMessage(msg: msg, to: view)
    }
    class func showMessage(msg:String!, to view:UIView) {
        self.showMessage(msg: msg, to: view, complete: nil)
    }
    class func showMessage(msg:String!, to view:UIView, complete:XBlock?) {
        self.showIcon(nil, msg: msg, to: view, complete: complete)
    }
    class func showIcon(_ icon:UIImage?, msg:String!, to view:UIView, complete:XBlock?) {
        
        func moveLastOne() {
            let hudList = view.subviews.filter({$0 is MBProgressHUD})
            for (i, value) in hudList.reversed().enumerated() {
                let hud = value as! MBProgressHUD
                if i > 2 {
                    hud.hide(animated: true)
                }else{
                    UIView.animate(withDuration: 0.3) {
                        hud.transform = CGAffineTransform.init(a: 1 - CGFloat(i + 1) * 0.2, b: 0, c: 0, d: 1 - CGFloat(i + 1) * 0.2, tx: 30 * CGFloat(i + 1), ty: -0.1 * CGFloat(i + 1))
                        hud.alpha = 1 - CGFloat(i + 1) * 0.2
                    }
                }
            }
        }
        moveLastOne()
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.isUserInteractionEnabled = false
        hud.animationType = .zoom
        hud.bezelView.style = MBProgressHUDBackgroundStyle.solidColor;
        hud.bezelView.backgroundColor = UIColor.init(white: 0, alpha: 0.6)
        if icon != nil {
            hud.mode = MBProgressHUDMode.customView
            hud.customView = UIImageView.init(image: icon)
        }else{
            hud.mode = MBProgressHUDMode.text
        }
        hud.label.textColor = .white
        hud.label.text = msg
        hud.label.font = UIFont.font(14)
        hud.margin = 15
        hud.offset.y = 150
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: 1.5)
        hud.completionBlock = complete
    }
    class func showLoading() {
        guard let view = self.getFatherView() else {return}
        if self.shared.loadingHUD != nil {
            self.shared.loadingHUD?.hide(animated: false)
        }
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.bezelView.style = MBProgressHUDBackgroundStyle.solidColor;
        hud.bezelView.backgroundColor = UIColor.clear
        hud.mode = MBProgressHUDMode.indeterminate
        hud.backgroundColor = UIColor.clear
        hud.label.textColor = .white
        hud.margin = 15
        hud.offset.y = 0
        hud.removeFromSuperViewOnHide = true
        self.shared.loadingHUD = hud
    }
    class func hideLoading() {
        if self.shared.loadingHUD != nil {
            self.shared.loadingHUD?.hide(animated: false)
        }
    }
    class func getFatherView() -> UIView?{
        var view : UIView? = UIApplication.shared.windows.first
        if view == nil {
            view = UIApplication.shared.rootViewController?.view
        }
        if view == nil {
            print("HUD找不到父界面")
        }
        return view
    }
}
class HudImageView:UIImageView {
    var realSize : CGSize = CGSize(width: 50, height: 50)
    override var intrinsicContentSize: CGSize {
        return realSize
    }
}
extension XXHudManager {
    class func afterLoadingRandom(do block: @escaping (()->())) {
        self.showLoading()
        let second: TimeInterval = TimeInterval(arc4random() % 1000 + 500) / 1000
        afterLoading(second, do: block)
    }
    class func afterLoading(_ second: TimeInterval, do block: @escaping (()->())) {
        self.showLoading()
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(Int(second * 1000))) {
            self.hideLoading()
            block()
        }
    }
}
